/*id为check-btn的*/
$('#check-btn').click(function () {
    var docContent = $('#docContent').val();
    if (check(docContent)) layer.msg('格式正确！');
});


$('#edit-btn').click(function () {
    var docContent = $('#docContent').val();
    check(docContent);
});

function uploadDocContent(docContent, docId) {
    $.ajax({
        url: '/reference/update/content',
        type: 'PUT',
        data: {
            docContent: docContent,
            docId: docId
        },
        success: function (data) {
            if (data.code == 0)
                layer.msg('修改成功！');
        }
    })
}


/*格式检查*/
function check(docContent) {
    if (isNotEmpty(docContent)) {
        var data = new Object();
        $.ajaxSettings.async = false;
        $.post("/reference/check", {docContent: docContent},
            function (result, status) {
                data = result.data;
            });
        $.ajaxSettings.async = true;
        if (data.code == 0) return true;
        else {
            layer.alert("格式错误！<br>" + data.message);
            return false;
        }
    }
}

function loadTable(tableData) {
    var table = layui.table;
    //执行一个 table 实例
    table.render({
        elem: '#demo'
        , height: 420
        , data: tableData
        , title: '用户表'
        , page: false //开启分页
        , toolbar: false //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , totalRow: false //开启合计行
        , cols: [[ //表头
            {field: 'id', hide: 'true'}
            , {field: 'docContent', title: '文献内容', width: 859}
            , {fixed: 'right', width: 165, align: 'center', toolbar: '#barDemo'}
        ]]
    });
}

function upload() {
    var formData = new FormData();
    formData.append("file", $('#file')[0].files[0]);
    $.ajax({
        url: '/reference/upload',
        dataType: 'html',
        type: 'POST',
        async: false,
        data: formData,
        processData: false, // 使数据不做处理
        contentType: false, // 不要设置Content-Type请求头
        success: function (data) {
            $('#upload-box').hide();
            var data = JSON.parse(data);
            loadTable(data.data);
        }
    });
}

function appendTable() {
    var docContent = $('#docContent').val(),
        layer = layui.layer;
    if ($.isEmptyObject(docContent)) {
        layer.msg("文献内容不能为空！");
    } else {
        var tableData = layui.table.cache['demo'];
        tableData.push({
            id: null,
            docContent: docContent,
            userId: null,
            aggregateId: null,
            updateTime: null,
            userId: null
        })
        loadTable(tableData);
        layer.msg('添加成功');
        $('#docContent').val('');
    }
}

function downloadTable() {
    var tableData = layui.table.cache['demo'],
        rowData = tableData.map(function (v) {
            return v.docContent;
        });
    var documents = ["Saab", "Volvo", "BMW"];
    var url = '/reference/download';
    $.fileDownload(url, {
        httpMethod: 'get',
        //dataType: 'json',
        //contentType: 'application/json',
        data: JSON.stringify(documents),
    });
}


/*输入是否为不空*/
function isNotEmpty(obj) {
    if (obj == '') {
        layer.msg('输入为空！');
        return false;
    } else return true;
}